/**   
*
* @创建人：Liuyong   
* @创建时间：2013-11-5 下午01:18:05   
*    
*/ 
package com.crmsys.login.controller;

import java.security.interfaces.RSAPublicKey;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.crmsys.auth_framework.auth.model.CUser;
import com.crmsys.auth_framework.auth.service.IUserInfoService;
import com.crmsys.common.constant.AuthConstants;
import com.crmsys.common.constant.Constant;
import com.crmsys.common.util.Decode64;
import com.crmsys.common.util.JSONUtil;
import com.crmsys.common.util.SessionUtil;
import com.crmsys.common.weather.Weather;

/**   
 *
 * @描述：   
 * @创建人：Liuyong   
 * @创建时间：2013-11-5 下午01:18:05   
 * @修改人：Liuyong   
 * @修改时间：2013-11-5 下午01:18:05   
 * @修改备注：   
 * @version 1.0
 *    
 */
@Controller
@SessionAttributes(value={AuthConstants.PARENT_MENU,AuthConstants.CHILD_MENU})
public class IndexController {
	@Autowired
	private IUserInfoService iUserInfoService;
	/**
	 * 
	*@return
	* @描述：   跳转到框架页，同时到toIntegrate中去验证用户的有效性
	* @创建人：Liuyong   
	* @创建时间：2013-11-5 下午01:19:34   
	* @修改人：Liuyong   
	* @修改时间：2013-11-5 下午01:19:34   
	* @修改备注：   
	* @version 1.0
	*
	 */
	@RequestMapping(value="/index")
    public String index(HttpServletRequest request,Model model){
		//数据传输加密
		RSAPublicKey pk = SessionUtil.genRandom(request);
		model.addAttribute("random_pk", pk.getModulus().toString(16));
		model.addAttribute("random_pe", pk.getPublicExponent().toString(16));
    	return "login/login";
    }
	
	/**
	 * 
	*@param model
	*@return
	* @描述：   登陆成功跳转到框架
	* @创建人：Liuyong   
	* @创建时间：2013-11-7 下午05:35:42   
	* @修改人：Liuyong   
	* @修改时间：2013-11-7 下午05:35:42   
	* @修改备注：   
	* @version 1.0
	 * @throws Exception 
	*
	 */
	@RequestMapping(value="/toIntegrateForword")
	public String toIntegrateForword(ModelMap model,HttpServletRequest request,String r) throws Exception{
		CUser cuser = SessionUtil.getLoginUserInfo(request);
		String roleid = new String(Decode64.decode(r));
		//获取菜单资源
		Map<String,List> mapResource = iUserInfoService.getResourceByRoleId(roleid);
		//构建菜单
		List listParent = null;
		List listChild = null;
		if(!mapResource.isEmpty()){
			listParent = mapResource.get("listParent");
			listChild = mapResource.get("listChild");
		}
		String menuResource = "";
		if(!listParent.isEmpty()&&listParent!=null){
			for(int i=0,k=listParent.size();i<k;i++){
				String str="";
				Map obj = (Map)listParent.get(i);
				str +="<li><a href=\"#\" class=\"icon_a\">"+obj.get("name")+"</a><ul style=\"position:relative; bottom:30px;\">";
				for(int l=0,m=listChild.size();l<m;l++){
					Map o = (Map)listChild.get(l);
					if(obj.get("csid").toString().equals(o.get("parentid").toString())){
				    //菜单五个参数
					str+="<li><a href=\"###\" onclick=\"javaScript:addTab('"+o.get("link")+"','"+o.get("name")+"','','"+1+"','"+o.get("raid")+"');\" title=\""+o.get("name")+"\">"+o.get("name")+"</a></li>";
					}
				}
				str+="</ul></li>";
				menuResource +=str;
			}
		}
		model.put("menuResource",menuResource);//菜单
		model.put("name", cuser.getName());//用户姓名
		model.put(AuthConstants.PARENT_MENU, listParent);//一级菜单
		model.put(AuthConstants.CHILD_MENU, listChild);//子菜单
		return "frame/frame";
	}
	
	/**
	 * 
	*@param model
	*@return
	* @描述：   跳转到中心页
	* @创建人：Liuyong   
	* @创建时间：2013-11-7 下午06:28:26   
	* @修改人：Liuyong   
	* @修改时间：2013-11-7 下午06:28:26   
	* @修改备注：   
	* @version 1.0
	 * @throws Exception 
	*
	 */
	@RequestMapping(value="/toCenter")
	public String toCenter(Model model,HttpServletRequest request) throws Exception{
		//将天气数据放到页面
		int provinceCode = Weather.getProvinceCode("四川");
		int cityCode = Weather.getCityCode(provinceCode, "成都");
		List<String> weatherList = Weather.getWeather(cityCode);
		if(!weatherList.isEmpty()&&weatherList.size()>1){
			SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
			model.addAttribute(Constant.PHOTO, "b_"+weatherList.get(11));
			model.addAttribute(Constant.CITY, weatherList.get(1));
			model.addAttribute(Constant.TEMPERATURE, weatherList.get(8));
			model.addAttribute(Constant.HHMM, weatherList.get(3).subSequence(11, 16));
		}
		//parentmenu数据
		List listParent = SessionUtil.getParentMenu(request);
		//childmenu数据
		List listChild = SessionUtil.getChildMenu(request);
		String jsonParent = JSONUtil.getJsonStrFromList(listParent);
		String jsonChild =  JSONUtil.getJsonStrFromList(listChild);
		model.addAttribute("jsonParent", jsonParent);
		model.addAttribute("jsonChild", jsonChild);
	    return "frame/center";	
	}
}
